home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 258 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: library.erc.clarkson.edu!rpi!not-for-mail
  2. From: Alexander Stepanov <stepanov@murrow.corp.sgi.com>
  3. Newsgroups: comp.lang.c++.moderated,comp.lang.c++,comp.os.ms-windows.programmer.misc
  4. Subject: Re: STL loops through a list with one element more than once!  Why?
  5. Date: 3 Jan 1996 09:26:38 -0000
  6. Organization: Silicon Graphics, Inc.
  7. Sender: cppmods@netlab.cs.rpi.edu
  8. Approved: kanze@gabi-soft.fr
  9. Message-ID: <4cdi4e$cer@netlab.cs.rpi.edu>
  10. References: <4c1n8o$2jn@netlab.cs.rpi.edu> <4c96ot$1ve@netlab.cs.rpi.edu>
  11. NNTP-Posting-Host: netlab.cs.rpi.edu
  12.  
  13. X-Original-Date: 2 Jan 1996 19:18:24 GMT
  14.  
  15. terris@rahul.net (Terris Linenbach) wrote:
  16. >X-Original-Date: 1 Jan 1996 05:46:03 GMT
  17. >
  18. >"erase" invalidates all active iterators on the container.  
  19.  
  20. I think it is not quite true. Erase invalidates some active iterators on the
  21. container. In case of list, erase invalidates only those iterators that point
  22. to the erased elements.
  23.  
  24. In this particular case:
  25.  
  26.     for (_itemIter  =   _itemList.begin();
  27.          _itemIter  !=  _itemList.end();
  28.          _itemIter++)  // <- itemIter is invalid now
  29.     {
  30.        _itemList.erase (_itemIter);
  31.     }
  32.  
  33. But there is a simple fix:
  34.  
  35.    for (_itemIter  =   _itemList.begin();
  36.          _itemIter  !=  _itemList.end(); )
  37.     {
  38.        _itemList.erase (_itemIter++);
  39.     }
  40.  
  41. and, of course,
  42.  
  43.     _itemList.erase(_itemList.begin(),  _itemList.end());
  44.  
  45. will do the same thing.
  46.  
  47.  
  48.  
  49. Alex Stepanov
  50. stepanov@mti.sgi.com
  51.  
  52. Silicon Graphics
  53. 2011 N. Shorline Boulevard
  54. Mountain View, CA 94043-1389
  55.  
  56. tel.: (415)933-6121
  57.  
  58.  
  59.     [ comp.lang.c++.moderated is a moderated newsgroup.  Submit articles ]
  60.     [  to <c++-submit@netlab.cs.rpi.edu>.  The moderation policy can be  ]
  61.     [   retrieved from <http://netlab.cs.rpi.edu/~cppmods/guide.html>.   ]
  62.     [    Moderators can be reached at: c++-request@netlab.cs.rpi.edu.    ]
  63.